﻿using System;
using System.Collections.Generic;
using System.Text;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace shudiangis.Common
{
    public class UserColorSetting
    {
        /// <summary>
        /// 为层设置颜色
        /// </summary>
        /// <param name="pLayer"></param>
        /// <param name="color"></param>
        public void SetLayerColor(IFeatureLayer pLayer, IColor color)
        {
            ISymbol pSym=null;
            ISimpleRenderer pRen;

            //根据layer类型设置不同的Symbol和颜色 
            switch (pLayer.FeatureClass.ShapeType)
            {
                case esriGeometryType.esriGeometryPoint:

                    IMarkerSymbol pPointSym = GetLayerSymbol(pLayer) as IMarkerSymbol;
                    pPointSym.Color = color;
                    pSym = pPointSym as ISymbol;

                    break;

                case esriGeometryType.esriGeometryPolyline:

                    ILineSymbol pLineSym = GetLayerSymbol(pLayer) as ILineSymbol;
                    pLineSym.Color = color;
                    pSym = pLineSym as ISymbol;

                    break;

                case esriGeometryType.esriGeometryPolygon:

                    IFillSymbol pPolygonSym = GetLayerSymbol(pLayer) as IFillSymbol;

                    if( pPolygonSym==null)
                    {
                        pPolygonSym = new SimpleFillSymbol();
                    }

                    pPolygonSym.Color = color;
                    pSym = pPolygonSym as ISymbol;
                    
                    break;
            }

            pRen = new SimpleRenderer();
            pRen.Symbol = pSym;

            IGeoFeatureLayer pGeoLayer = pLayer as IGeoFeatureLayer;
            pGeoLayer.Renderer = pRen as IFeatureRenderer;
        }

        /// <summary>
        /// 获取层的符号
        /// </summary>
        /// <param name="pLayer"></param>
        /// <returns></returns>
        public ISymbol GetLayerSymbol(IFeatureLayer pLayer)
        {
            IFeatureRenderer pRen;
            IGeoFeatureLayer pGeoLayer;
            IFeature pFea;
            IFeatureCursor pCursor;

            pGeoLayer = pLayer as IGeoFeatureLayer;
            pRen = pGeoLayer.Renderer;
            pCursor = pLayer.FeatureClass.Search(null, false);

            //得到FeatureClass中的第一个Feature 
            pFea = pCursor.NextFeature();

            if (pFea == null)
            {
                while (pFea == null)
                {
                    pFea = pCursor.NextFeature();
                }
            }

            //得到featurelayer使用的Symbol 
            return pRen.get_SymbolByFeature(pFea);
        }
    }
}
